<!-- DHI取样 -->
<template>
    <!-- 筛选条件 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="企业名称" v-if="companySelect">
            <el-select filterable clearable v-model="formInline.companyId">
                <el-option v-for="item in companyNameList" :label="item.name" :value="item.id" :key="item.id" />
            </el-select>
        </el-form-item>

        <el-form-item label="选择牛号">
            <el-select clearable filterable v-model="formInline.cowId">
                <el-option v-for="item in allComNameList" :label="`${item.code}-${item.breed}-${item.genderName}`" :value="item.id" />
            </el-select>
        </el-form-item>

        <el-form-item>
            <el-button icon="Search" @click="search">查询</el-button>
            <el-button v-if="uploadButton" type="primary" icon="Upload" @click="isShowUpload = true">导入</el-button>
            <el-button type="success" icon="Download">导出</el-button>
        </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" stripe border style="width: 100%;">
        <el-table-column prop="cowCode" label="牛号" width="350" fixed="left">
            <template #default="scope">
                <div style="display: flex;">
                    <span>{{ scope.row.cowCode }}</span>
                    <!-- 性别 -->
                    <el-tag v-if="scope.row.genderName == '公牛'" style="margin-left: auto;" effect="dark" type="primary" size="small">{{ scope.row.genderName }}</el-tag>
                    <el-tag v-if="scope.row.genderName == '母牛'" style="margin-left: auto;" effect="dark" type="danger" size="small">{{ scope.row.genderName }}</el-tag>
                    <!-- 品种 -->
                    <el-tag style="margin: 0 5px;" effect="dark" type="success" size="small">{{ scope.row.breed }}</el-tag>
                    <!-- 年龄 -->
                    <el-tag effect="dark" type="info" size="small">{{ scope.row.age }}</el-tag>
                </div>
            </template>
        </el-table-column>
        <el-table-column v-if="companyColumn" prop="companyName" label="企业名称" width="150" show-overflow-tooltip />
        <el-table-column prop="birthDate" label="出生日期" width="130" />
        <el-table-column prop="parity" label="胎次(胎)" width="130" />
        <el-table-column prop="samplingDate" label="采样日期" width="130" />
        <el-table-column prop="calvingDate" label="产犊日期" width="130" />
        <el-table-column prop="calvingInterval" label="产犊间隔(天)" width="130" />
        <el-table-column prop="lactationDays" label="泌乳天数(天)" width="130" />
        <el-table-column prop="milkProduction" label="产奶量(kg)" width="130" />
        <el-table-column prop="fatRate" label="乳脂率(%)" width="130">
            <template #default="scope">
                <div style="display: flex; align-items: center;">
                    <span style="margin-right: 5px;">{{ scope.row.fatRate }}</span>
                    <el-icon color="red">
                        <Download />
                    </el-icon>
                </div>
            </template>
        </el-table-column>
        <el-table-column prop="proteinRate" label="蛋白率(%)" width="130" />
        <el-table-column prop="fatProteinRatio" label="脂蛋比" />
        <el-table-column prop="somaticCellCount" label="体细胞数(万/ml)" width="150">
            <template #default="scope">
                <div style="display: flex; align-items: center;">
                    <span style="margin-right: 5px;">{{ scope.row.somaticCellCount }}</span>
                    <el-icon color="blue">
                        <Upload />
                    </el-icon>
                </div>
            </template>
        </el-table-column>
        <el-table-column prop="somaticCellGrade" label="体细胞数分" width="130" />
        <el-table-column prop="ureaNitrogen" label="尿素氮(mg/dl)" width="150" />
        <el-table-column prop="milkLoss" label="奶损率(kg)" width="130" />
        <el-table-column prop="milkPriceDifference" label="奶款差" width="130" />
        <el-table-column prop="economicLoss" label="经济损失" width="130" />
        <el-table-column prop="correctedMilk" label="校正奶(kg)" width="130" />
        <el-table-column prop="persistence" label="持续力" width="130" />
        <el-table-column prop="whi" label="WHI" width="130" />
        <el-table-column prop="previousMilk" label="前奶量(kg)" width="130" />
        <el-table-column prop="previousSomaticCellCount" label="前体细胞数(万/ml)" width="150" />
        <el-table-column prop="previousSomaticCellGrade" label="前体细胞数分" width="130" />
        <el-table-column prop="previousMilkLoss" label="前奶量损失(kg)" width="150" />
        <el-table-column prop="peakMilk" label="高峰奶(kg)" width="130" />
        <el-table-column prop="peakDay" label="高峰日(天)" width="130" />
        <el-table-column prop="milkIn305" label="305奶量(kg)" width="130" />
        <el-table-column prop="totalProduction" label="总奶量(kg)" width="130" />
        <el-table-column prop="totalFat" label="总乳脂(%)" width="130" />
        <el-table-column prop="totalProtein" label="总蛋白(%)" width="130" />
        <el-table-column prop="adultEquivalent" label="成年当量(kg)" width="130" />
        <el-table-column prop="createUserName" label="记录人" width="100" />
        <el-table-column prop="createTime" label="记录日期" width="180" />
        <el-table-column label="操作" fixed="right" width="100">
            <template #default="scope">
                <el-button v-if="deleteButton" type="danger" icon="Delete" size="small">删除</el-button>
                <!-- <el-button type="primary" icon="Edit" size="small">编辑</el-button>
                <el-button type="info" icon="Warning" size="small" @click="abnormalHandler">数据异常</el-button> -->
            </template>
        </el-table-column>
    </el-table>
    <br/>
    <!-- 分页器 -->
    <el-pagination
        v-model:currentPage="pageNo"
        v-model:page-size="limit"
        :page-sizes="[15, 30, 50, 100]"
        :small="false"
        :background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="total"
        @current-change="getCowDhiPage"
        @size-change="sizeChange"
    />

    <!-- 导入-->
    <upload :isShowUpload="isShowUpload" @closeUploadDrawer="closeUploadDrawer"></upload>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { reqGetCompanyNameList } from '@/api/company'
import { reqGetCowDhiPage } from '@/api/daily'
import { reqCowDhiCowSelect } from '@/api/publicCowSelect'
import { ElMessageBox } from 'element-plus'
import upload from './upload/index.vue'
// 引入用户相关小仓库
import useUserStore from '@/store/modules/user'
let userStore = useUserStore()
const companyColumn = userStore.componentConfig.companyColumn // 企业名称列-是否显示
const companySelect = userStore.componentConfig.companySelect // 企业下拉-是否显示
const addButton = userStore.componentConfig.addButton // 新增按钮-是否显示
const deleteButton = userStore.componentConfig.deleteButton // 删除按钮-是否显示
const editButton = userStore.componentConfig.editButton // 编辑按钮-是否显示
const errorButton = userStore.componentConfig.errorButton // 数据异常按钮-是否显示
const uploadButton = userStore.componentConfig.uploadButton // 导入按钮-是否显示

// 分页器当前页码
let pageNo = ref(1)
// 每一页展示多少条数据
let limit = ref(15)
// 数据总条数
let total = ref(0)

// 导入弹窗，显示隐藏
const isShowUpload = ref(false)
// 关闭导入弹窗
const closeUploadDrawer = (val) => {
    isShowUpload.value = val
    getCowDhiPage()
}

// 表单数据收集
const formInline = reactive({
    companyId: '', // 企业id
    cowId: '', // 牛号
    sortName: '', // 排序字段名称
    sortType: '', // 排序类型：ascending-正序、descending-倒序
})

// 搜索按钮
const search = () => {
    pageNo.value = 1
    getCowDhiPage()
}

// 获取dhi取样的列表数据
const tableData = ref([])
const getCowDhiPage = async () => {
    let res = await reqGetCowDhiPage(Object.assign(formInline, {
        pageNumber: pageNo.value, // 页号
        pageSize: limit.value // 每页数量
    }))
    if(res.status == 200){
        total.value = res.data.totalRow
        tableData.value = res.data.records
    }
}

// 获取牛号下拉列表
let allComNameList = ref([])
const cowDhiCowSelect = async () => {
    let res = await reqCowDhiCowSelect()
    allComNameList.value = res.data
    console.log('gagagag', res)
}

// 获取企业下拉列表数据
let companyNameList = ref([])
const getCompanyNameList = async () => {
    let res = await reqGetCompanyNameList()
    companyNameList.value = res.data
}

// 异常按钮
const abnormalHandler = () => {
    ElMessageBox.prompt('请输入异常原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        inputPattern: /^.+$/,
        inputErrorMessage: '请填写异常原因'
    }).then((res) => {
            console.log('异常原因为：', res.value)
    }).catch(() => {
            console.log('取消填写异常原因')
    })
}

// 分页的下拉菜单发生改变时触发
const sizeChange = () => {
    pageNo.value = 1
    getCowDhiPage()
}

// 组件挂载完毕
onMounted(() => {
    cowDhiCowSelect()
    getCowDhiPage()
    getCompanyNameList()
})
</script>

<style scoped lang="scss">
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>